<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="用户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysUserId">
              <j-search-select-tag type="list" v-model="model.sysUserId" dict="sys_user,realname,id" placeholder="请选择用户" :disabled="canEdit"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="订单编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderNum">
              <a-input v-model="model.orderNum" placeholder="请输入订单编号" disabled=true ></a-input>
            </a-form-model-item>
          </a-col>
          
          <a-col :span="12">
            <a-form-model-item label="订单名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderName">
              <a-input v-model="model.orderName" placeholder="请输入订单名称" :disabled="canEdit"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="订单状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderStatus">
              <j-dict-select-tag type="list" v-model="model.orderStatus" dictCode="order_status" placeholder="请选择订单状态" :disabled="canEdit"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="订单来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderSource">
              <j-dict-select-tag type="list" v-model="model.orderSource" dictCode="order_source" placeholder="请选择订单来源" :disabled="canEdit"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="付款方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="payType">
              <j-dict-select-tag type="list" v-model="model.payType" dictCode="pay_type" placeholder="请选择付款方式" :disabled="canEdit"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="预留姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="uname">
              <a-input v-model="model.uname" placeholder="请输入预留姓名" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item v-show="!(operRole && canEdit)" label="预留电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="uphone">
              <a-input v-model="model.uphone" placeholder="请输入预留电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="付款时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="payTime">
              <j-date placeholder="请选择付款时间"  v-model="model.payTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="canEdit"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item v-has="'seatorder:orderMoney'" label="订单金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderMoney">
              <a-input v-model="model.orderMoney" placeholder="请输入订单金额" :disabled="canEdit"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime">
              <j-date placeholder="请选择开始时间"  v-model="model.startTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <j-date placeholder="请选择结束时间"  v-model="model.endTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="自习室" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zixishiId">
              <j-dict-select-tag type="list" v-model="model.zixishiId" dictCode="sr_zixishi,name,id" placeholder="请选择自习室" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="区域" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quyuId">
              <j-dict-select-tag type="list" v-model="model.quyuId" dictCode="sr_quyu,name,id" placeholder="请选择区域" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="座位号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seatId">
              <j-dict-select-tag type="list" v-model="model.seatId" :dictCode="`sr_seat,name,id,(quyu_id = '${model.quyuId}' )`" placeholder="请选择座位号" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="订单类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reserveType">
              <j-dict-select-tag type="list" v-model="model.reserveType" dictCode="reserve_type" placeholder="请选择订单类型" :disabled="canEdit"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import Vue from 'vue'
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { queryUserRoleInfo} from '@/api/api'
  import { USER_INFO } from '@/store/mutation-types'
  export default {
    name: 'OrderForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        canEdit: true,
        operRole: false,
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/order/order/add",
          edit: "/order/order/edit",
          queryById: "/order/order/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      let user = Vue.ls.get(USER_INFO)
      console.log(user);
      this.getUserRoles(user.id)
    },
    methods: {
      getUserRoles(userid){
        queryUserRoleInfo({userid:userid}).then((res)=>{
          if(res.success){
            res.result.forEach(item =>{
              if(item.roleCode == 'operations_role'){
                this.operRole = true;
                return;
              }
            })
          }
        });
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        if(this.model.id){
          this.canEdit = true;
        }else{
          this.canEdit = false;
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>